<?php
class Block_Admin_Attribute_View_Item
    extends Block_Core_Form_Abstract
{
    protected $_formName = 'attribute_view_form';

    protected $_formId = 'attribute_view_form';

    protected $_jsName = 'attribute_view';

    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('form/attribute/template.phtml');
    }

    protected function _prepareForm()
    {
        $fieldset = new Block_Core_Form_Fieldset_Abstract();
        $fieldset->setData(array('label'=>'Attribute Informaton','title'=>'Attribute Informaton','view'=>'open'));

        $fieldset->addField(array(
            'type' => 'hidden',
            'name' => 'attribute[id]',
            'value'=> $this->getId(),
        ));

        $fieldset->addField(array(
            'title'=>'Attribute Name',
            'label'=>'Attribute Name',
            'type' =>'text',
            'value'=> $this->getName(),
            'name' =>'attribute[name]',
            'required'=>true,
            'comment'=>'Attribute Name',
        ));

        $fieldset->addField(array(
            'title'=>'Attribute Code',
            'label'=>'Attribute Code',
            'type' =>'text',
            'name' =>'attribute[code]',
            'value'=> $this->getCode(),
            'required'=>true,
            'comment'=>'Attribute Code',
        ));

        $fieldset->addField(array(
            'title'=>'Input Type',
            'label'=>'Input Type',
            'type' =>'select',
            'value'=> $this->getInputType(),
            'name' =>'attribute[input_type]',
            'required'=>true,
            'id'   =>'input_type',
            'params'=> Admin_Attribute_InputType::getOptionArray(),
        ));

        $fieldset->addField(array(
            'title'=>'Type',
            'label'=>'Type',
            'type' =>'select',
            'id'   =>'backend_type',
            'value'=> $this->getType(),
            'name' =>'attribute[type]',
            'required'=>true,
            'params'=>Admin_Attribute_Type::getOptionArray(),
        ));

        $fieldset->addField(array(
            'title'=>'Apply to',
            'label'=>'Apply to',
            'value'=> $this->getEntityType(),
            'type' =>'select',
            'name' =>'attribute[entity_type]',
            'required'=>true,
            'params'=>Admin_Attribute_EntityType::getOptionArray(),
        ));

        $this->addFieldset($fieldset);

        return parent::_prepareForm();

    }

    public function _prepareData()
    {
    /* @var $controller Zend_Controller_Front */
        $controller = Zend_Controller_Front::getInstance();
        $item = $controller->getRequest()->getParam('item',null);

        if($item) {
            $model = new Model_Eav_Attribute();
            $model->load($item);

            foreach ($model->getData() as $key=>$value) {
                $this->setData($key,$value);
            }
        }

        return parent::_prepareData();
    }
}